﻿using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using sBlog.Net.Domain.Entities;
using sBlog.Net.Domain.Interfaces;

namespace sBlog.Net.Domain.Concrete
{
    public class Follow : DefaultDisposable, IFollow
    {
        private readonly Table<FollowEntity> _followTable;

        public Follow()
        {
            _followTable = context.GetTable<FollowEntity>();
        }

        public List<FollowEntity> GetFollowByFollowingUserID(int followingUserID)
        {
            return _followTable.Where(c => c.FollowingUserID == followingUserID).ToList();
        }

        public FollowEntity GetByFolowingUserIDAndFolowerUserID(int followingUserID, int followerUserId)
        {
            return _followTable.Where(c => c.FollowingUserID == followingUserID && c.FollowerUserID == followerUserId).FirstOrDefault();
        }

        public List<FollowEntity> GetFollowByFollowerUserID(int followerUserID)
        {
            return _followTable.Where(c => c.FollowerUserID == followerUserID).ToList();
        }

        public void DeleteFollowByFollowID(int followID)
        {
            var follow = _followTable.Where(c => c.FollowID == followID);
            if (follow.Any())
            {
                _followTable.DeleteAllOnSubmit(follow);
                context.SubmitChanges();
            }
        }

        public void AddFollow(FollowEntity followEntity)
        {
            _followTable.InsertOnSubmit(followEntity);
            context.SubmitChanges();
        }

        ~Follow()
        {
            Dispose(false);
        }
    }
}
